/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package SIO_Client;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 *
 * @author Teva
 */
public class StandardClient 
{
    private static long start;
    private static long end;
    private static String Filepath;
    static int BufferSize = 4000512;
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws IOException, InterruptedException 
    {
         
        writeResult("================================");
        writeResult("SIO Approach Testing");
        DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd  HH:mm:ss");
        Date date = new Date();
        writeResult("Test Date  " + dateFormat.format(date));
        writeResult("Included: [Prefetching.]");
        writeResult("Buffer Size =>" + BufferSize);
        writeResult("================================");
        int j;
        for(j=11;j<12;j++) //this loop is used to crawl 7 files
         { 
           StringBuilder FP = new StringBuilder("/Users/teva/Desktop/DataFile/File").append(j).append(".txt"); 
           Filepath = FP.toString();
           Prefecthing(Filepath);
             for(int i=1;i<5;i++) //To run the same code 10 times.
              {
                
                writeResult("Iteration No =>" + i +  " And File name is =>" + " File" + j + " Total Time =>" + SendFile(Filepath,BufferSize));
                 
              } 
           }
    } 
    
    static long totalTime;
    protected static long SendFile(String Filepath,int BufferSize) throws IOException, InterruptedException
    {
       OutputStream os = null;
       FileInputStream fis = null;
       Socket sock = null;
        try 
        {
            System.out.println("Client I/O initiating...");
            sock = new Socket("10.141.11.125",3289);
            System.out.println("Client connected to server successfully!");
            os = sock.getOutputStream();
            File input = new File(Filepath);
            fis = new FileInputStream(input);
            byte[] data = new byte[BufferSize];
            int len=0;  
            start = System.currentTimeMillis();
            while((len=fis.read(data))>0) 
            {
                os.write(data, 0, len);
                os.flush();               
            }
            long end = System.currentTimeMillis();
            totalTime = end-start;
          } 
        
       finally
       {
         
          fis.close();
          sock.close();
          os.close();
          
       }
        return totalTime;
    }
  
     public static void writeResult(String Results) throws IOException
    {
         PrintWriter out = null;
    try 
    {
        FileWriter outFile = new FileWriter("/Users/teva/Desktop/SIOResults.txt",true);
        out = new PrintWriter(outFile);

        // Write text to file       
        out.println(Results);        
       
    } 
    finally
    {
        out.close(); 
    }
   
      
}
     
    public static void Prefecthing(String FilePath) throws FileNotFoundException, IOException 
    {
      
      FileInputStream fis = null;
      File input = new File(Filepath);
      fis = new FileInputStream(input);
      byte[] data = new byte[BufferSize];
      int len=0;  
      while((len=fis.read(data))>0) {}
    }
}